package service

import (
	"encoding/json"
	"fmt"
	"rongyy/src/app"
	"rongyy/src/helper"
	"rongyy/src/middlewares"
	"rongyy/src/models"
	"time"
)

type WxConfService struct {
}

// 挂号成功通知
func (ps *WxNoticeService) RegSuccess(req *models.ReqNoticeParams) error {
	bi := &models.BusinessInfo{
		PatName:         req.PatName,
		DocName:         req.DocName,
		PatHospitalId:   req.PatHospitalId,
		DepartmentName:  req.DepartmentName,
		AppointmentTime: req.AppointmentTime,
		RedirectPage: models.RedirectPage{
			PageType: "mini_program",
			AppId:    "wx490cfcb50c67fb33",
			Fullpath: "/pages/index/index?navTitle=toPaid",
		},
	}
	req.OpenId = req.OpenId
	req.OrderId = req.PatHospitalId + time.Now().String()
	err := ps.SendNotice(req, 1501001, bi, "挂号成功通知")
	if err != nil {
		middlewares.Logger.Println("挂号成功通知 error==== ", err)
		//return err
	}
	return nil
}

func (ps *WxConfService) GetToken() (string, error) {
	mpt := &models.MiniProgramToken{}
	err := mpt.First()
	if err != nil {
		middlewares.Logger.Println(err)
	}
	d := time.Now().Sub(time.Time(mpt.UpdatedAt))
	// 减600秒 提前十分钟判断是否更新token
	middlewares.Logger.Println("d.Seconds()", d.Seconds(), mpt.ExpiresIn-600, int(d.Seconds()) < mpt.ExpiresIn-600)
	if int(d.Seconds()) < mpt.ExpiresIn-600 {
		return mpt.Token, nil
	}
	req := &models.ReqAccessToken{
		GrantType: "client_credential",
		Appid:     app.GetEnv("MP_APP_ID"),
		Secret:    app.GetEnv("MP_APP_SECRET"),
	}
	url := fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/stable_token")
	result, err := helper.PostJson(url, req)
	if err != nil {
		return "", err
	}
	respMiniToken := &models.RespMiniToken{}
	err = json.Unmarshal(result, respMiniToken)
	if err != nil || respMiniToken.ErrCode != 0 {
		middlewares.Logger.Println("Insert Unmarshal failed", err, respMiniToken.ErrMsg)
		return "", err
	}
	mpt.Token = respMiniToken.AccessToken
	mpt.ExpiresIn = respMiniToken.ExpiresIn
	err = mpt.Update()
	if err != nil {
		return "", err
	}
	return mpt.Token, nil
}

func (ps *WxConfService) GetGzhToken() (string, error) {
	mpt := &models.MiniProgramToken{}
	err := mpt.GzhFirst()
	if err != nil {
		middlewares.Logger.Println(err)
	}
	d := time.Now().Sub(time.Time(mpt.UpdatedAt))
	// 减600秒 提前十分钟判断是否更新token
	middlewares.Logger.Println("d.Seconds()", d.Seconds(), mpt.ExpiresIn-600, int(d.Seconds()) < mpt.ExpiresIn-600)
	if int(d.Seconds()) < mpt.ExpiresIn-600 {
		return mpt.Token, nil
	}

	url := fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s", app.GetEnv("GZH_APP_ID"), app.GetEnv("GZH_APP_SECRET"))
	result, err := helper.Get(url)
	if err != nil {
		return "", err
	}
	respMiniToken := &models.RespMiniToken{}
	err = json.Unmarshal(result, respMiniToken)
	if err != nil || respMiniToken.ErrCode != 0 {
		middlewares.Logger.Println("Insert Unmarshal failed", err, respMiniToken.ErrMsg)
		return "", err
	}
	mpt.Token = respMiniToken.AccessToken
	mpt.ExpiresIn = respMiniToken.ExpiresIn
	err = mpt.UpdateGzh()
	if err != nil {
		return "", err
	}
	return mpt.Token, nil
}
